"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));

var _jsxRuntime = require("react/jsx-runtime");

var _proUtils = require("@ant-design/pro-utils");

var _antdV = require("antd-v4");

var _classnames = _interopRequireDefault(require("classnames"));

var _react = require("react");

var _Group = _interopRequireWildcard(require("./Group"));

require("./index.less");

var _excluded = ["prefixCls", "className", "avatar", "title", "description", "cover", "extra", "style"];

var CheckCard = function CheckCard(props) {
  var _classNames;

  var _useMountMergeState = (0, _proUtils.useMountMergeState)(props.defaultChecked || false, {
    value: props.checked,
    onChange: props.onChange
  }),
      _useMountMergeState2 = (0, _slicedToArray2.default)(_useMountMergeState, 2),
      stateChecked = _useMountMergeState2[0],
      setStateChecked = _useMountMergeState2[1];

  var checkcardGroup = (0, _react.useContext)(_Group.CheckCardGroupConnext);

  var _useContext = (0, _react.useContext)(_antdV.ConfigProvider.ConfigContext),
      getPrefixCls = _useContext.getPrefixCls;

  var handleClick = function handleClick(e) {
    var _props$onClick, _checkcardGroup$toggl;

    props === null || props === void 0 ? void 0 : (_props$onClick = props.onClick) === null || _props$onClick === void 0 ? void 0 : _props$onClick.call(props, e);
    var newChecked = !stateChecked;
    checkcardGroup === null || checkcardGroup === void 0 ? void 0 : (_checkcardGroup$toggl = checkcardGroup.toggleOption) === null || _checkcardGroup$toggl === void 0 ? void 0 : _checkcardGroup$toggl.call(checkcardGroup, {
      value: props.value
    });
    setStateChecked === null || setStateChecked === void 0 ? void 0 : setStateChecked(newChecked);
  }; // small => sm large => lg


  var getSizeCls = function getSizeCls(size) {
    if (size === 'large') return 'lg';
    if (size === 'small') return 'sm';
    return '';
  };

  (0, _react.useEffect)(function () {
    var _checkcardGroup$regis;

    checkcardGroup === null || checkcardGroup === void 0 ? void 0 : (_checkcardGroup$regis = checkcardGroup.registerValue) === null || _checkcardGroup$regis === void 0 ? void 0 : _checkcardGroup$regis.call(checkcardGroup, props.value);
    return function () {
      var _checkcardGroup$cance;

      return checkcardGroup === null || checkcardGroup === void 0 ? void 0 : (_checkcardGroup$cance = checkcardGroup.cancelValue) === null || _checkcardGroup$cance === void 0 ? void 0 : _checkcardGroup$cance.call(checkcardGroup, props.value);
    }; // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [props.value]);
  /**
   * 头像自定义
   *
   * @param prefixCls
   * @param cover
   * @returns
   */

  var renderCover = function renderCover(prefixCls, cover) {
    return (0, _jsxRuntime.jsx)("div", {
      className: "".concat(prefixCls, "-cover"),
      children: typeof cover === 'string' ? (0, _jsxRuntime.jsx)("img", {
        src: cover,
        alt: "checkcard"
      }) : cover
    });
  };

  var customizePrefixCls = props.prefixCls,
      className = props.className,
      avatar = props.avatar,
      title = props.title,
      description = props.description,
      cover = props.cover,
      extra = props.extra,
      _props$style = props.style,
      style = _props$style === void 0 ? {} : _props$style,
      others = (0, _objectWithoutProperties2.default)(props, _excluded);
  var checkCardProps = (0, _objectSpread2.default)({}, others);
  var prefixCls = getPrefixCls('pro-checkcard', customizePrefixCls);
  checkCardProps.checked = stateChecked;
  var multiple = false;

  if (checkcardGroup) {
    var _checkcardGroup$value;

    // 受组控制模式
    checkCardProps.disabled = props.disabled || checkcardGroup.disabled;
    checkCardProps.loading = props.loading || checkcardGroup.loading;
    checkCardProps.bordered = props.bordered || checkcardGroup.bordered;
    multiple = checkcardGroup.multiple;
    var isChecked = checkcardGroup.multiple ? (_checkcardGroup$value = checkcardGroup.value) === null || _checkcardGroup$value === void 0 ? void 0 : _checkcardGroup$value.includes(props.value) : checkcardGroup.value === props.value; // loading时check为false

    checkCardProps.checked = checkCardProps.loading ? false : isChecked;
    checkCardProps.size = props.size || checkcardGroup.size;
  }

  var _checkCardProps$disab = checkCardProps.disabled,
      disabled = _checkCardProps$disab === void 0 ? false : _checkCardProps$disab,
      size = checkCardProps.size,
      cardLoading = checkCardProps.loading,
      _checkCardProps$borde = checkCardProps.bordered,
      bordered = _checkCardProps$borde === void 0 ? true : _checkCardProps$borde,
      checked = checkCardProps.checked;
  var sizeCls = getSizeCls(size);
  var classString = (0, _classnames.default)(prefixCls, className, (_classNames = {}, (0, _defineProperty2.default)(_classNames, "".concat(prefixCls, "-loading"), cardLoading), (0, _defineProperty2.default)(_classNames, "".concat(prefixCls, "-").concat(sizeCls), sizeCls), (0, _defineProperty2.default)(_classNames, "".concat(prefixCls, "-checked"), checked), (0, _defineProperty2.default)(_classNames, "".concat(prefixCls, "-multiple"), multiple), (0, _defineProperty2.default)(_classNames, "".concat(prefixCls, "-disabled"), disabled), (0, _defineProperty2.default)(_classNames, "".concat(prefixCls, "-bordered"), bordered), _classNames));
  var metaDom = (0, _react.useMemo)(function () {
    if (cardLoading) {
      return (0, _jsxRuntime.jsx)(_Group.CardLoading, {
        prefixCls: prefixCls || ''
      });
    }

    if (cover) {
      return renderCover(prefixCls || '', cover);
    }

    var avatarDom = avatar ? (0, _jsxRuntime.jsx)("div", {
      className: "".concat(prefixCls, "-avatar"),
      children: typeof avatar === 'string' ? (0, _jsxRuntime.jsx)(_antdV.Avatar, {
        size: 48,
        shape: "square",
        src: avatar
      }) : avatar
    }) : null;
    var headerDom = (title || extra) && (0, _jsxRuntime.jsxs)("div", {
      className: "".concat(prefixCls, "-header"),
      children: [(0, _jsxRuntime.jsx)("div", {
        className: "".concat(prefixCls, "-title"),
        children: title
      }), extra && (0, _jsxRuntime.jsx)("div", {
        className: "".concat(prefixCls, "-extra"),
        children: extra
      })]
    });
    var descriptionDom = description ? (0, _jsxRuntime.jsx)("div", {
      className: "".concat(prefixCls, "-description"),
      children: description
    }) : null;
    var metaClass = (0, _classnames.default)("".concat(prefixCls, "-content"), (0, _defineProperty2.default)({}, "".concat(prefixCls, "-avatar-header"), avatarDom && headerDom && !descriptionDom));
    return (0, _jsxRuntime.jsxs)("div", {
      className: metaClass,
      children: [avatarDom, headerDom || descriptionDom ? (0, _jsxRuntime.jsxs)("div", {
        className: "".concat(prefixCls, "-detail"),
        children: [headerDom, descriptionDom]
      }) : null]
    });
  }, [avatar, cardLoading, cover, description, extra, prefixCls, title]);
  return (0, _jsxRuntime.jsx)("div", {
    className: classString,
    style: style,
    onClick: function onClick(e) {
      if (!cardLoading && !disabled) {
        handleClick(e);
      }
    },
    children: metaDom
  });
};

CheckCard.Group = _Group.default;
var _default = CheckCard;
exports.default = _default;